<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 
 * 路由类
 * 
 * @author lixn
 */

class Router{

	protected $c_routes = null;
	protected $path = '';
	
	protected $directory = '';
	protected $class = '';
	protected $method = 'index';

	protected $args;
	protected $route = array();
	protected $segments = array();
	protected $rsegments = array();
	
	public function __construct(){
		$this->c_routes = variable_get('route', array());

		$this->init_path();
		
		/**
		 * 方便其他地方实用~
		 * @var unknown_type
		 */
		$_GET['q'] = $this->path;
		
		$this->parse_route();
	}
	
	private function init_path(){
		$this->path = variable_get('default_controller', 'home');
		
		if(!empty($_GET['q'])){
			$this->path = trim($_GET['q'], '/');
		}
		
		if($suffix = variable_get("url_suffix", false)){
			$this->path = preg_replace("|".preg_quote($suffix)."$|", "", $this->path);
		}
		
		$this->path = strtolower($this->path);
	}
	
	/**
	 * 路由分析
	 */
	private function parse_route(){
//		$segments = explode($this->path, '/');
		
		$uri = $this->path;
		$this->segments = explode('/', $uri);

		if(!empty($this->c_routes)){
			
			if(isset($this->c_routes[$uri])){
				return $this->set_request(explode('/',$this->c_routes[$uri]));
			}
			
			//Loop through the route array looking for wild-cards
			foreach($this->c_routes as $key => $val){
				//Convert wild-cards to RegEx
				$key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key));
				
				//Does the RegEx match?
				if(preg_match('#^' . $key . '$#', $uri)){
					if(strpos($val, '$') !== FALSE && strpos($key, '(') !== FALSE){
						$val = preg_replace('#^'.$key.'$#', $val, $uri);
					}
					
					return $this->set_request(explode('/', $val));
				}
			}
		}
		
		$this->set_request($this->segments);
	}
	
	private function set_request($segments = array()){
		
		$segments = $this->validate_request($segments);
		
		$this->class = ucfirst($segments[0]) . 'Action';
		
		if(isset($segments[1])){
			$this->method = $segments[1];
		}else{
			$segments[1] = 'index';
		}
		
		$this->rsegments = $segments;
		$this->args = array_slice($segments, 2);
	}
	
	private function validate_request($segments){
		
		/*
		 * 以第一个参数为action class判断是否存在 
		 */
		if(file_exists(RUN_PATH . 'action/' . ucfirst($segments[0]) . 'Action.php')){
			return $segments;
		}
		
		if(is_dir(RUN_PATH . 'action/' . $segments[0])){
			$this->directory = $segments[0];
			array_shift($segments);
		 	
			if(count($segments) > 0){
				
				if(!file_exists(RUN_PATH . 'action/' . $this->directory . '/' . ucfirst($segments[0]) . 'Action.php')){
					show_404();
				}
				
				return $segments;
			}else{
				show_404();
			}
		}
		
		/*
		 * 不存在，显示404错误~退出程序
		 */
		show_404();
	}
	/*
	 * ---------------------------------------
	 * 对外接口
	 * ---------------------------------------
	 */
	public function getPath(){
		return $path;	
	}
	
	public function getDirectory(){
		return $this->directory;
	}
	
	public function getClass(){
		return $this->class;
	}
	
	public function getMethod(){
		return $this->method;
	}
	
	public function getArgs(){
		return $this->args;
	}
	
	public function getRoute(){
		if(!empty($this->route)){
			return $this->route;
		}
		
		if($this->directory){
			$this->route[] = $this->directory;
		}
		
		if(!empty($this->rsegments)){
			$this->route[] = $this->rsegments[0];
			$this->route[] = $this->rsegments[1];
		}
		
		return $this->route;
	}
}